package com.ruoyi.web.api;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ObjectUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.discuss.domain.SysDiscuss;
import com.ruoyi.discuss.service.IDiscussService;
import com.ruoyi.discuss.vo.DiscussVO;
import com.ruoyi.discuss.vo.ReplyVO;
import com.ruoyi.discuss.vo.SecondReplyVO;
import com.ruoyi.exam.vo.ScoresCombineVO;
import com.ruoyi.exam.vo.ScoresCourseVO;
import com.ruoyi.questionBank.domain.ExamPaper;
import com.ruoyi.questionBank.service.IExamPaperService;
import com.ruoyi.student.impl.StudentUserService;
import com.ruoyi.student.vo.UserInfoVO;
import com.ruoyi.web.controller.scores.ScoresController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/discuss")
public class DiscussApi extends BaseController {

    @Autowired
    private IDiscussService discussService;

    //评论列表
    @RequestMapping("list")
    public TableDataInfo discussList(DiscussVO discussVO){
        ObjectUtil.checkNull(discussVO.getCourseNo(),"课程编号为空");
        startPage();
        List<DiscussVO> list = discussService.listDiscuss(discussVO);
        return getDataTable(list);
    }
    //发布评论
    @RequestMapping("publish")
    public AjaxResult discuss( @RequestBody DiscussVO discussVO){
        ObjectUtil.checkNull(discussVO.getCourseNo(),"课程编号为空");
        ObjectUtil.checkNull(discussVO.getContent(),"评论内容为空");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        discussVO.setUserId(loginUser.getUserId());
        discussService.insertDiscuss(discussVO);
        return AjaxResult.success();
    }

    //回复评论
    @RequestMapping("reply")
    public AjaxResult reply( @RequestBody ReplyVO replyVO){
        ObjectUtil.checkNull(replyVO.getParentDiscussId(),"父评论ID为空");
        ObjectUtil.checkNull(replyVO.getContent(),"回复内容为空");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        replyVO.setUserId(loginUser.getUserId());
        discussService.reply(replyVO);
        return AjaxResult.success();
    }

    //二次回复
    @RequestMapping("secondReply")
    public AjaxResult secondReply( @RequestBody SecondReplyVO secondReplyVO){
        ObjectUtil.checkNull(secondReplyVO.getParentReplyId(),"父回复ID为空");
        ObjectUtil.checkNull(secondReplyVO.getContent(),"回复内容为空");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        secondReplyVO.setUserId(loginUser.getUserId());
        discussService.secondReply(secondReplyVO);
        return AjaxResult.success();
    }
    //点赞
    @RequestMapping("like")
    public AjaxResult like( @RequestBody DiscussVO discussVO){
        ObjectUtil.checkNull(discussVO.getDiscussId(),"评论ID为空");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysDiscuss discuss = discussService.updateLikeCount(discussVO.getDiscussId(),loginUser.getUserId());
        Map<String,String> resultMap = new HashMap<>();
        resultMap.put("likeCount",String.valueOf(discuss.getLikeCount()));
        resultMap.put("discussId",String.valueOf(discuss.getDiscussId()));
        resultMap.put("discussCount",String.valueOf(discuss.getDiscussCount()));
        return AjaxResult.success(resultMap);
    }



}
